<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addpermission({type:1,pid:'0'})">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="addpermission({type:2,pid:row.id})">添加</el-button>
              <el-button type="text" @click="etidoermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :visible="showDialog" title="弹层标题" @close="showDialog = false">
      <el-form label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { permissionAPI, addPermissionAPI, getimportExcelAPI, updatePermissionAPI, delPermissionAPI } from '@/api/Permission'
import tree from '@/utils/transTree'
export default {
  data() {
    var validateCode = (rule, value, callback) => {
      // const filterlist =
      const codeList = this.codeList.map(item => item.code)
      console.log(codeList)
      if (codeList.includes(value)) {
        callback(new Error('code重复'))
      } else {
        callback()
      }
    }
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: null, // 类型 1级 type=1 2级 type=2
        pid: '' // 点击上面的权限添加 传固定值0  如果点击表格内的要以当前id为准
      },
      rules: {
        code: [
          { validator: validateCode, trigger: 'blur' }
        ]
      },
      codeList: []
    }
  },
  mounted() {
    this.permission()
  },
  methods: {
    async permission() {
      const res = await permissionAPI()
      this.list = tree(res)
      this.codeList = res
    },
    addpermission({ type, pid }) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    closeDialog() {
      this.showDialog = false
      this.formDatacode()
    },
    async confirm() {
      if (this.formData.id) {
        await updatePermissionAPI(this.formData)
      } else {
        await addPermissionAPI(this.formData)
      }
      this.permission()
      this.showDialog = false
      this.formDatacode()
    },
    formDatacode() {
      this.formData = {
        enVisible: '', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: null, // 类型 1级 type=1 2级 type=2
        pid: '' //
      }
    },
    async etidoermission(id) {
      const res = await getimportExcelAPI(id)
      this.formData = res
      this.showDialog = true
    },
    async delPermission(id) {
      await delPermissionAPI(id)
      this.permission()
    }

  }
}
</script>
